var $;
layui.config({
	base : "../../js/"
}).use(['form','layer'],function(){
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery;

    var orgId = window.sessionStorage.getItem("organizeId");
    var memberNum = window.sessionStorage.getItem("memberNumEditId");
    console.log("memeberNum="+memberNum)
    $(".userName").val(memberNum)
    $.get($.httpRootPath()+"/api/web/department/i/"+orgId+"/dep-deaf",function(data){
    	console.log("department");
    	data =  JSON.parse(data);
    	if(data.code != "00"){
    		layer.msg("获取部门列表失败！");
    		$("#submitBtn").attr("disabled","disabled")
    		return;
    	}
        var msgHtml = '';
        msgHtml += '<select name="userGrade" lay-filter="required" class = "depOpt">'
        data = data.data;
        for(var i=0; i<data.length; i++){
            msgHtml += '<option value="'+data[i].id+'" >'+data[i].name+'</option>';
        }
        msgHtml += " </select>"
        $(".depSelect").html(msgHtml);
//      form.render('select');
// 		$("#depSelect").append(msgHtml);

    })
    $.get($.httpRootPath()+"/api/web/profession/i/"+orgId,function(data){
    		console.log("profession");
    	data =  JSON.parse(data);
    	if(data.code != "00"){
    		layer.msg("获取职称列表失败！");
    		$("#submitBtn").attr("disabled","disabled");
    		return;
    	}
        var msgHtml = '';
        msgHtml += '<select name="userStatus" lay-filter="required" class = "proOpt">'
        
        data = data.data;
        for(var i=0; i<data.length; i++){
            msgHtml += '<option name ="proChose" value="'+data[i].id+'" >'+data[i].name+'</option>';
        }
        msgHtml += " </select>"
        $(".proSelect").html(msgHtml);
//      form.render('select');
// 		$("#depSelect").append(msgHtml);

    })
     $.get($.httpRootPath() + "/api/web/member/i/" + orgId + "/" + memberNum,function(data){
    	data =  JSON.parse(data);
    	console.log("member")
    	if(data.code == "00"){
    		var depId = data.data.department.id;
    		var proId = data.data.profession.id;
//           $(" option[value='"+depId+"']").attr("selected","selected");
			$("[name='userGrade']").find("option[value="+depId+"]").attr("selected",true);  
// 			$("[name='userGrade']").val(depId);
// 			$("[name='userStatus']").val(proId);
 			$("[name='userStatus']").find("option[value="+proId+"]").attr("selected",true);  
   			form.render('select');

    	}
    })
    
 	form.on("submit(subInfo)",function(data){
 	
 		var data ={
 			memberNum:$(".userName").val(),
 			pid :$(".proOpt").val(),
 			did:$(".depOpt").val(),
 			orgId:orgId
 		}
 		console.log(data);
 		//弹出loading
 		var index = layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
 		updateInfo(data,function(){
 			layer.close(index);
			top.layer.msg("成员信息修改成功！");
 			layer.closeAll("iframe");
 		})
//	 	parent.location.reload();
	 	console.log("parent.location.reload()")
 		return false;
 	})
})


function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}
function updateInfo(data,callback) {
	data._method= 'PUT'
    $.ajax({
        url: $.httpRootPath()+'/api/web/organize/i/'+data.orgId+"/pro-dep",
        type: 'POST',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            if (r.code == "00") {
              	layer.msg("修改成功！");
            } else {
               layer.msg(r.msg);
            }
        },
        error: function (e) {
          layer.msg("系统错误！");
        },
        complete:function(){
        	callback();
        }
    });

}


